home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Borland / Borland C++ V5.02 / STDLIB.PAK / HEAP_OPS.CPP < prev    next >
Text File  |  1997-05-06  |  2KB  |  74 lines

  1.  #include <algorithm>
  2.  #include <vector>
  3.  
  4.  using namespace std;
  5.  
  6.  int main ()
  7.  {
  8.    int d1[4] = {1,2,3,4};
  9.    int d2[4] = {1,3,2,4};                
  10.    //
  11.    // Set up two vectors.
  12.    //
  13.    vector<int> v1(d1+0, d1+4), v2(d2+0, d2+4);
  14.    //
  15.    // Make heaps.
  16.    //
  17.    make_heap(v1.begin(), v1.end());
  18.    make_heap(v2.begin(), v2.end(), less<int>());
  19.    //
  20.    // v1 = (4,x,y,z)  and  v2 = (4,x,y,z)
  21.    //
  22.    // Note that x, y and z represent the remaining values in the
  23.    // container (other than 4).  The definition of the heap and heap
  24.    // operations  does not require any particular ordering
  25.    // of these values.
  26.    //
  27.    // Copy both vectors to cout.
  28.    //
  29.    ostream_iterator<int> out(cout," ");
  30.    copy(v1.begin(), v1.end(), out);
  31.    cout << endl;
  32.    copy(v2.begin(), v2.end(), out);
  33.    cout << endl;
  34.    //
  35.    // Now let's pop.
  36.    //
  37.    pop_heap(v1.begin(), v1.end());
  38.    pop_heap(v2.begin(), v2.end(), less<int>());
  39.    //
  40.    // Copy both vectors to cout.
  41.    //
  42.    copy(v1.begin(), v1.end(), out);
  43.    cout << endl;
  44.    copy(v2.begin(), v2.end(), out);
  45.    cout << endl;
  46.    //
  47.    // And push.
  48.    //
  49.    push_heap(v1.begin(), v1.end());
  50.    push_heap(v2.begin(), v2.end(), less<int>());
  51.    //
  52.    // Copy both vectors to cout.
  53.    //
  54.    copy(v1.begin(),v1.end(),out);
  55.    cout << endl;
  56.    copy(v2.begin(),v2.end(),out);
  57.    cout << endl;
  58.    //
  59.    // Now sort those heaps.
  60.    //
  61.    sort_heap(v1.begin(), v1.end());
  62.    sort_heap(v2.begin(), v2.end(), less<int>());
  63.    //
  64.    // Copy both vectors to cout.
  65.    //
  66.    copy(v1.begin(), v1.end(), out);
  67.    cout << endl;
  68.    copy(v2.begin(), v2.end(), out);
  69.    cout << endl;
  70.  
  71.    return 0;
  72.  }
  73.  
  74.